package com.android.fragmentdemo
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.android.fragmentdemo.ScrollingActivity.Companion
import com.android.fragmentdemo.adapters.StaggeredLayoutManagerAdapter
import com.android.fragmentdemo.adapters.StaggeredLayoutManagerAdapter2
import com.hengfoxwolf.commonbase.base.BaseActivity
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.ActivityStaggeredBinding
import com.android.fragmentdemo.util.StepLoadCollectionUtil
import java.util.Random

class StaggeredActivity : BaseActivity<ActivityStaggeredBinding>() {

    private val mRandom by lazy {
        Random()
    }

    private lateinit var mTotalDataSet: MutableList<String>

    override fun getLayoutId(): Int = R.layout.activity_staggered

    override fun init(savedInstanceState: Bundle?) {
//        val dataSet = DataFactoryIp.createDataSet()
//        val bottomData = mutableListOf<Int>()
//        val baseHeight = resources.getDimension(R.dimen.dp_150).toInt()
//        for (i in 0.until(dataSet.size)) {
//            bottomData.add(baseHeight + mRandom.nextInt(200))
//        }
//        val mAdapter = StaggeredLayoutManagerAdapter(this@StaggeredActivity, bottomData)

        mTotalDataSet = DataFactoryIp.createSimpleItemData(86)
        StepLoadCollectionUtil.init(mTotalDataSet, 25)

        val mAdapter = StaggeredLayoutManagerAdapter2(this@StaggeredActivity)
        mAdapter.itemDataSet = StepLoadCollectionUtil.getInitDataSet()
        mBinding.rvStaggered.apply {
            layoutManager = StaggeredGridLayoutManager(SPAN_COUNT, StaggeredGridLayoutManager.VERTICAL)
            adapter = mAdapter

            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)

                    if (recyclerView.layoutManager is StaggeredGridLayoutManager) {
                        val dataArray = IntArray(SPAN_COUNT)
                        val positionArray =
                            ((recyclerView.layoutManager) as? StaggeredGridLayoutManager)?.findLastCompletelyVisibleItemPositions(dataArray) ?: dataArray
                        if (positionArray.isNotEmpty()) {
                            Log.d(TAG_TAG, "positionArray:${positionArray.size}")
                            Log.d(TAG_TAG, "positionArray0:${positionArray[0]}")
                            Log.d(TAG_TAG, "positionArray1:${positionArray[1]}")

                            if ((positionArray[0] == mAdapter.itemCount - 1 || positionArray[1] == mAdapter.itemCount - 1)
                                && !StepLoadCollectionUtil.hadLoadFinished()) {
                                val mLoadDataSet = StepLoadCollectionUtil.loadItem()
                                if (mLoadDataSet.size > 0) {
                                    mAdapter.setItemDataSet(mLoadDataSet)
                                }
                            }
                        }
                    }
                }

                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                }
            })
        }
    }

    override fun onStop() {
        super.onStop()
        StepLoadCollectionUtil.reset()
    }

    companion object {
        const val TAG_TAG = "StaggeredActivity"
        const val SPAN_COUNT = 2
    }
}